package fun.flyee.android.pengshan.acts;

import android.Manifest;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;
import fun.flyee.android.pengshan.Constant;
import fun.flyee.android.pengshan.Storage;
import fun.flyee.android.pengshan.acts.helper.ApkHelper;
import fun.flyee.android.pengshan.acts.helper.ServiceHelper;
import fun.flyee.android.pengshan.fgts.WebFragment;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.android.pengshan.widgets.Tips;
import fun.flyee.pengshan.social.R;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.demo.VideoActivity;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class MainActivity extends BaseActivity {

    private long exitTime = 0;

    private WebFragment webFragment;

    private ApkHelper apkHelper;
    private ServiceHelper serviceHelper;

    @Override
    protected int layoutId() {
        return R.layout.act_container;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            webFragment = WebFragment.newInstance("/main", "");
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.frame_container, webFragment, "fragment_web").show(webFragment);
            transaction.commitAllowingStateLoss();
        } else {
            webFragment = (WebFragment) getSupportFragmentManager().findFragmentByTag("fragment_web");
            webFragment.setPath("/main");
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.show(webFragment);
            transaction.commitAllowingStateLoss();
        }
        Systems.checkAndGoFloatPermission(this);
        apkHelper = new ApkHelper(this);
        apkHelper.checkVersion();
        if (!TextUtils.isEmpty(Storage.get(Storage.TOKEN)) && Systems.getCPUABI().contains("arm")) {
            serviceHelper = ServiceHelper.getInstance(this);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == Constant.INTENT_REQUEST_INSTALL_PERMISSION) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.INSTALL_PACKAGES) != PackageManager.PERMISSION_GRANTED) {
                Systems.requestInstallPermission(this);
                return;
            } else {
                apkHelper.install();
            }
        } else {
            Systems.requestPermission(this);
        }
    }

    @Override
    public void onBackPressed() {
        if (webFragment != null) {
            webFragment.onBackPressed();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onPostCreate(Bundle bundle) {
        super.onPostCreate(bundle);
        Systems.requestPermission(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 1000) {
                Tips.toast("再次点击将退出应用");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
            }
        }
        return false;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent bundle) {
        super.onActivityResult(requestCode, resultCode, bundle);
        if (webFragment != null) {
            webFragment.onActivityResult(requestCode, resultCode, bundle);
        }
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void onDestroy() {
        if (serviceHelper != null) {
            serviceHelper.destroy();
            serviceHelper = null;
        }
        super.onDestroy();
    }

}
